package com.yuju.controller.group;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.group.enums.MemberReturnCodeEnum;
import com.yuju.group.model.MemberModel;
import com.yuju.group.service.IMemberService;
import com.yuju.util.Utils;
import com.yuju.vo.group.MemberVO;

/**
 * 
 * 类名：MemberController  <br />
 *
 * 功能：会员信息相关接口
 *
 * @author : zhousc <br />
 * @Date : 2017年11月24日 上午9:44:36  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("member")
public class MemberController extends BaseController<MemberVO, MemberModel> {

	@Autowired
	private IMemberService memberService;
	
	/**
	 * 
	 * 功能：<br/> 会员信息列表
	 *
	 * @author zhousc
	 * @Date 2017年11月23日 下午2:05:13 <br/>
	 */
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject obj) {
		MemberVO memberVO = obj.toJavaObject(MemberVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(memberVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		};
		//会员信息集团共享
		memberVO.setHotelId(null);
		//模糊查询
		if(Utils.isNotNull(memberVO.getQ())){
			memberVO.setMyLike_name(memberVO.getQ());
		}
		Result result = new Result();
		PageModel pageModel = memberService.pageList(convert(memberVO), memberVO.getPage(), memberVO.getPageSize());
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(MemberReturnCodeEnum.MERBER_QUERY_SUCCESS.getCode());
			result.setReturnMsg(MemberReturnCodeEnum.MERBER_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>会员添加
	 *
	 * @author zhousc
	 * @Date 2017年11月23日 下午2:30:05 <br/>
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj) {
		MemberVO memberVO = obj.toJavaObject(MemberVO.class);
		Result result = new Result();
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(memberVO, "save", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		};
				
		int rows = memberService.save(convert(memberVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(MemberReturnCodeEnum.MERBER_SAVE_SUCCESS.getCode());
			result.setReturnMsg(MemberReturnCodeEnum.MERBER_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(MemberReturnCodeEnum.MERBER_SAVE_FAILURE.getCode());
			result.setReturnMsg(MemberReturnCodeEnum.MERBER_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>会员更新
	 *
	 * @author zhousc
	 * @Date 2017年11月23日 下午2:30:26 <br/>
	 */
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject obj) {
		MemberVO memberVO = obj.toJavaObject(MemberVO.class);
		Map<String, String> fail = FieldValidator.validateAll(memberVO, "update", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		};
		Result result = new Result();
		int rows = memberService.updateSelective(convert(memberVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(MemberReturnCodeEnum.MERBER_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(MemberReturnCodeEnum.MERBER_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(MemberReturnCodeEnum.MERBER_UPDATE_FAILURE.getCode());
			result.setReturnMsg(MemberReturnCodeEnum.MERBER_UPDATE_FAILURE.getDesc());
		}
		return result;
	}

	/**
	 * 
	 * 功能：<br/>会员删除
	 *
	 * @author zhousc
	 * @Date 2017年11月23日 下午2:32:02 <br/>
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject obj) {
		MemberVO memberVO = obj.toJavaObject(MemberVO.class);
		Map<String, String> fail = FieldValidator.validateAll(memberVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		};
		Result result = new Result();
		int rows = memberService.del(memberVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(MemberReturnCodeEnum.MERBER_DEL_SUCCESS.getCode());
			result.setReturnMsg(MemberReturnCodeEnum.MERBER_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(MemberReturnCodeEnum.MERBER_DEL_FAILURE.getCode());
			result.setReturnMsg(MemberReturnCodeEnum.MERBER_DEL_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>根据ID查询会员信息
	 *
	 * @author zhousc
	 * @Date 2017年11月23日 下午2:34:12 <br/>
	 */
	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject obj) {
		MemberVO memberVO = obj.toJavaObject(MemberVO.class);
		Map<String, String> fail = FieldValidator.validateAll(memberVO, "get", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		};
		Result result = new Result();
		MemberModel memberModel = memberService.findById(memberVO.getId());
		if (Utils.isNotNull(memberModel)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(memberModel);
			result.setReturnCode(MemberReturnCodeEnum.MERBER_QUERY_SUCCESS.getCode());
			result.setReturnMsg(MemberReturnCodeEnum.MERBER_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
}
